<html>
    <head>
               <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
               <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
<?php
require_once 'model/db.php';
?>


<?php
$query = "SELECT * FROM companies";
$companies = getData($query);
?>
<?php
$query = "SELECT * FROM apps";
$apps = getData($query);
?>
<form method="POST">
    Min fans<input type="text" name="min_fans" value =" 1"><br>
    Max fans<input type="text" name="max_fans" value =" 10"><br>
    Keyword <input type="text" name="keyword" value =" cafe"><br>
    Company: <select name="id_company">
        <option value="0">chon company</option>
        <?php foreach ($companies as $company): ?>
            <option value="<?php echo $company['id'] ;?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select><br>
    App: <select name="id_app">
               <option value="0">chon app</option>
                <?php foreach ($apps as $app) : ?>
                    <option value="<?php echo $app["id"]; ?>"><?php echo $app["name"]; ?></option>
                <?php endforeach; ?>
            </select>
    <input type="submit" name="submit">
</form>


<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans'];
    $max_fans = $_POST['max_fans'];
    $keyword = $_POST['keyword'];
    $companies = $_POST['id_company'];
    $apps = $_POST['id_app'];
   
     if (!empty($min_fans)) 
            $min_fans="(likes >= $min_fans) AND";
        else
            $min_fans="";  
        if (!empty($max_fans)) 
            $max_fans="(likes <= $max_fans) AND";
        else
            $max_fans=""; 
        if(empty($keyword))
            $keyword= "";
        if(!empty($companies))
            $companies="(companies.id=$companies) AND";
        else
            $companies="";
         if(!empty($apps))
            $apps="(apps.id=$apps) AND";
        else
            $apps="";
        
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE 
                        $min_fans
                        $max_fans
                        $companies
                        $apps
              description LIKE '% $keyword %'
                  LIMIT 0, 15 ";
                var_dump($query);
            $pages = getData($query);
        endif;
    ?>


    

    <?php if (!empty($pages)): ?>
        <table border="1">
            <tr>
                
                <td> id </td>
                <td> name</td>
                <td> profile </td>
                <td> likes </td>
                <td> description</td>
                                           
            </tr>
        <?php foreach ($pages as $page): ?>
                <tr>
                    <td><?php echo $page['id'] ?></td>
                    <td><?php echo $page['name'] ?></td>
                    <td><img src="<?php echo $page['picture'] ?>"></td>
                    <td><?php echo $page['likes'] ?></td>
                    <td><?php echo substr ($page['description'],0,100)."....." ;?></td>
                    
                    
       <?php // var_dump($page);die();  ?>
                </tr>
        <?php endforeach; ?>
        </table> 
            <?php endif; ?>
</html>